<?php
/**
 * Created by PhpStorm.
 * User: mayn
 * Date: 2018/8/31
 * Time: 11:46
 */

namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\model\Article as ArticleModel;
use app\api\validate\article\ArticleCreateOrEdit;
use app\common\validate\IDMustBePositiveInt;
use app\common\validate\PaginationValidate;
use app\lib\exception\ArticleException;

class Article extends BaseController
{
    public function createArticle() {
        $validate = new ArticleCreateOrEdit();
        $validate->goCheck(['scene' => 'create']);
        $params = $validate->getParameters();
        $result = ArticleModel::create($params);
        return $this->returnHandle($result);
    }

    public function deleteArticle($id) {
        (new IDMustBePositiveInt())->goCheck();
        $result = ArticleModel::destroy([$id]);
        return $this->returnHandle($result);
    }

    public function updateArticle() {
        $validate = new ArticleCreateOrEdit();
        $validate->goCheck();
        $params = $validate->getParameters();
        $result = ArticleModel::updateArticle($params);
        return $this->returnHandle($result);
    }

    public function readArticle($id) {
        (new IDMustBePositiveInt())->goCheck();
        $article = ArticleModel::readArticle($id);
        if (!$article) {
            throw new ArticleException([
                'msg' => 'The required article is not found'
            ]);
        }
        return $this->returnHandle($article);
    }

    public function listArticle($page, $size) {
        (new PaginationValidate())->goCheck();
        $article = ArticleModel::listArticle($page, $size);
        return $article;
    }
}